package com.jin10.schedule;

import cn.hutool.core.date.DateUtil;
import com.jin10.entity.SentenceLibrary;
import com.jin10.service.ISentenceLibraryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author raofy
 * @date 2022-03-04 11:52
 * @desc
 */
@Component
public class Task {

    private final static Logger log = LoggerFactory.getLogger(Task.class);

    @Autowired
    ISentenceLibraryService iSentenceLibraryService;
    /**
     * 每天定时处理失去时效的段落
     */
    @Scheduled(cron = "0 0 21 * * ?")
    public void cleanExpiredParagraph() {
        ArrayList<Integer> ids = new ArrayList<>();
        long currentTimestamp = DateUtil.date().getTime();
        List<SentenceLibrary> list = iSentenceLibraryService.list();
        if (list != null && !list.isEmpty()) {
            for (SentenceLibrary sentenceLibrary : list) {
                Date effectiveTime = sentenceLibrary.getEffectiveTime();
                if (currentTimestamp - effectiveTime.getTime() >= 0) {
                    ids.add(sentenceLibrary.getId());
                }
            }
        }
        if (!ids.isEmpty()) {
            log.info("过期的句子id有{}", Arrays.toString(ids.toArray()));
            iSentenceLibraryService.removeByIds(ids);
        }
    }
}
